# -*- coding: utf-8 -*-

import os
import ppc.pipe
import ppc.extensions.mail
import ppc.extensions.url


class Printer(ppc.pipe.Pipe):
    """标准输出"""

    def handle(self):
        v = self['format'].value
        print(v)

        self.transfer(self.input)


class Mailer(ppc.pipe.Pipe):
    """邮件发送"""

    def handle(self):
        mail = {}
        for k in ['from', 'to', 'subject', 'body']:
            mail[k] = self[k].value

        host = {}
        for k in ['host', 'user', 'pass']:
            host[k] = self[k].value

        ppc.extensions.mail.send(mail, host)

        self.transfer(self.input)


class UrlPoster(ppc.pipe.Pipe):
    """
    数据提交管道
    以http协议的multipart/form-data格式提交数据到网站接口
    """

    def handle(self):
        url = self['url'].value
        fields = self['fields'].value

        files = []
        if 'files' in self:
            files = self['files'].value
            if not isinstance(files, list):
                files = [files]

        if self['test'].value == 'True':
            self.transfer(fields)
        else:
            html = ppc.extensions.url.multipost(url, fields, files)
            self.transfer(html)
